package com.sweet.imserver;

import io.vertx.core.*;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.file.FileSystem;
import io.vertx.core.logging.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class Demo {

  public Demo() {
    System.out.println("init");
  }

  public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InterruptedException {
//    Class<?> xxxx = Class.forName(Demo.class.getName());
//    Object o = xxxx.newInstance();
//    System.out.println(o);
//
//    Class<?> aClass = ClassLoader.getSystemClassLoader().loadClass(Demo.class.getName());
//    System.out.println(aClass.newInstance());
//
//    List<String> copyOnWriteArrayList = new CopyOnWriteArrayList<>();
//    copyOnWriteArrayList.add("11");
//    copyOnWriteArrayList.add("22");
//
//    int size = copyOnWriteArrayList.size();
//    System.out.println(size);
//    for (String s : copyOnWriteArrayList) {
//      if ("22".equals(s)) {
//        copyOnWriteArrayList.remove(s);
//      }
//    }

//    System.out.println(copyOnWriteArrayList);

//    Vertx vertx = Vertx.vertx(new VertxOptions().setEventLoopPoolSize(1));
//
//    vertx.setTimer(1L, t -> {
//      print("timer1");
//      Promise<String> promise = Promise.succeededPromise("hello");
//      promise.future().setHandler(f -> {
//        print(f.result());
//      });
//      Promise<String> promise1 = Promise.succeededPromise("hello1");
//      promise1.future().setHandler(f -> {
//        print(f.result());
//      });
//    });
//    vertx.setTimer(1L, t -> {
//      print("timer2");
//      Promise<String> promise = Promise.succeededPromise("hello2");
//      promise.future().setHandler(f -> {
//        print(f.result());
//      });
//    });
//
//    Thread.sleep(2000L);

    Vertx vertx = Vertx.vertx();
    FileSystem fileSystem = vertx.fileSystem();
    Promise<Buffer> promise3 = Promise.promise();
    Promise<Buffer> promise4 = Promise.promise();

    fileSystem.readFile("pom.xml", promise3);
    fileSystem.readFile("note.txt", promise4);

    CompositeFuture.all(promise3.future(), promise4.future())
    .setHandler(r -> {
      for (Object o : r.result().list()) {
        System.out.println(o);
        System.out.println("---------");
      }
    });

  }

  private static void print(String s) {
    String s1 = Thread.currentThread().getName() + " : " + s;
    System.out.println(s1);
  }

  @Override
  public String toString() {
    return "Demo toString";
  }
}


